//
//  FaQiHeMaiViewController.m
//  PlayBall
//
//  Created by yanfa on 16/3/28.
//
//

#import "FaQiHeMaiViewController.h"



@interface FaQiHeMaiViewController ()

@end

@implementation FaQiHeMaiViewController

@synthesize playTypeId;
//佣金比例
@synthesize BonusScale;
/** 保密设置 */
@synthesize SecrecyLevel;
/** 认购金额 */
@synthesize offer;
/** 保底金额 */
@synthesize guarantee;
/** 总金额 */
//@synthesize totalMoney;
/** 倍数 */
@synthesize bs;
/** 单倍金额 */
@synthesize allMoney;
/** 期次ID */
@synthesize issueID;
/** 期次 */
@synthesize issueName;

/** 彩票ID */
@synthesize lotID;
@synthesize lotteryNumber;
/** 最小认购金额 */
int leastMoney = 0;


SHARED_INSTANCE_DEFINE(FaQiHeMaiViewController);

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    BonusScale = 0;
    SecrecyLevel = 0;
    offer = 0;
    guarantee = 0;
    leastMoney = 0;
    
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    customLab.backgroundColor = [UIColor clearColor];
    [customLab setText:@"发起合买"];
    customLab.textAlignment = NSTextAlignmentCenter;
    
    [customLab setTextColor:[UIColor whiteColor]];
    customLab.font = [UIFont systemFontOfSize:19];
    self.navigationItem.titleView = customLab;
    
//    //self.navigationItem.hidesBackButton = YES;
    
    UIImage *btnBgNormalImg = [[UIImage imageNamed:@"unselected"] resizableImageWithCapInsets:UIEdgeInsetsMake(17, 17, 2, 2)];
    UIImage *btnBGSelectImg = [[UIImage imageNamed:@"selected"] resizableImageWithCapInsets:UIEdgeInsetsMake(17, 17, 2, 2)];
    [_btnBd0 setBackgroundImage:btnBgNormalImg forState:UIControlStateNormal];
    [_btnBd0 setBackgroundImage:btnBGSelectImg forState:UIControlStateSelected];
    [_btnBd1 setBackgroundImage:btnBgNormalImg forState:UIControlStateNormal];
    [_btnBd1 setBackgroundImage:btnBGSelectImg forState:UIControlStateSelected];
    [_btnBd2 setBackgroundImage:btnBgNormalImg forState:UIControlStateNormal];
    [_btnBd2 setBackgroundImage:btnBGSelectImg forState:UIControlStateSelected];
    [_btnBd3 setBackgroundImage:btnBgNormalImg forState:UIControlStateNormal];
    [_btnBd3 setBackgroundImage:btnBGSelectImg forState:UIControlStateSelected];
    [_btnBd4 setBackgroundImage:btnBgNormalImg forState:UIControlStateNormal];
    [_btnBd4 setBackgroundImage:btnBGSelectImg forState:UIControlStateSelected];
    [_btnBd5 setBackgroundImage:btnBgNormalImg forState:UIControlStateNormal];
    [_btnBd5 setBackgroundImage:btnBGSelectImg forState:UIControlStateSelected];
    [_btnBd6 setBackgroundImage:btnBgNormalImg forState:UIControlStateNormal];
    [_btnBd6 setBackgroundImage:btnBGSelectImg forState:UIControlStateSelected];
    [_btnBd7 setBackgroundImage:btnBgNormalImg forState:UIControlStateNormal];
    [_btnBd7 setBackgroundImage:btnBGSelectImg forState:UIControlStateSelected];
    [_btnBd8 setBackgroundImage:btnBgNormalImg forState:UIControlStateNormal];
    [_btnBd8 setBackgroundImage:btnBGSelectImg forState:UIControlStateSelected];
    [_btnBd9 setBackgroundImage:btnBgNormalImg forState:UIControlStateNormal];
    [_btnBd9 setBackgroundImage:btnBGSelectImg forState:UIControlStateSelected];
    [_btnBd10 setBackgroundImage:btnBgNormalImg forState:UIControlStateNormal];
    [_btnBd10 setBackgroundImage:btnBGSelectImg forState:UIControlStateSelected];
    
    [_btnBmBaomi setBackgroundImage:btnBgNormalImg forState:UIControlStateNormal];
    [_btnBmBaomi setBackgroundImage:btnBGSelectImg forState:UIControlStateSelected];
    [_btnBmFllowGongkai setBackgroundImage:btnBgNormalImg forState:UIControlStateNormal];
    [_btnBmFllowGongkai setBackgroundImage:btnBGSelectImg forState:UIControlStateSelected];
    [_btnBmGongkai setBackgroundImage:btnBgNormalImg forState:UIControlStateNormal];
    [_btnBmGongkai setBackgroundImage:btnBGSelectImg forState:UIControlStateSelected];
    [_btnBmXiaoshouHgongkai setBackgroundImage:btnBgNormalImg forState:UIControlStateNormal];
    [_btnBmXiaoshouHgongkai setBackgroundImage:btnBGSelectImg forState:UIControlStateSelected];
    [_btnBmXSHFllowGongkai setBackgroundImage:btnBgNormalImg forState:UIControlStateNormal];
    [_btnBmXSHFllowGongkai setBackgroundImage:btnBGSelectImg forState:UIControlStateSelected];
    
    _TV_xuanyan.layer.borderColor = UIColor.grayColor.CGColor;
    _TV_xuanyan.layer.borderWidth = 0.4;
    _TV_xuanyan.layer.cornerRadius = 2;
    _TV_xuanyan.layer.masksToBounds = YES;
    
    [self.tfBaoDi addTarget:self action:@selector(BDtextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.tfRenGou addTarget:self action:@selector(RGtextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    if(leastMoney == 0){
        leastMoney = (int)ceilf(allMoney * 0.05f);
//        offer = Math.ceil(totalMoney * 0.05f);
//        guarantee = 0.00f;
        offer = (int)ceilf(allMoney * 0.05f);
        if(leastMoney <= 0){
            leastMoney = 1;
            offer = 1;
        }
        guarantee = 0;
        SecrecyLevel = 0;
        BonusScale = 0;
        [self setDefaultValue2Show];
        [self setYongjin];
        [self setSecrecy];
//    }
}
-(void)setDefaultValue2Show{
    [_btn_quanBao setSelected:false];
    [_labelIssueName setText:_issueNameTitle];
    [_label_TotalMoney setText:[NSString stringWithFormat:@"%d彩豆",allMoney]];
    [_tfRenGou setText:[NSString stringWithFormat:@"%d",offer]];
    [_tfBaoDi setText:[NSString stringWithFormat:@"%d",guarantee]];
    [_label_baodiPer setText:[NSString stringWithFormat:@"(%.2f%%)",roundf(guarantee*10000.0f/allMoney)/100]];
    [_label_rengouPer setText:[NSString stringWithFormat:@"(%.2f%%)",roundf(offer*10000.0f/allMoney)/100]];
    [_label_FaTotalMoney setText:[NSString stringWithFormat:@"共%d彩豆",(offer+guarantee)]];
    [_label_buyMoneyDetail setText:[NSString stringWithFormat:@"认购%d彩豆+保底%d彩豆",offer,guarantee]];
}
-(void)setvalue2Show{
    [_tfRenGou setText:[NSString stringWithFormat:@"%d",offer]];
    [_tfBaoDi setText:[NSString stringWithFormat:@"%d",guarantee]];
    [_label_baodiPer setText:[NSString stringWithFormat:@"(%.2f%%)",roundf(guarantee*10000.0f/allMoney)/100]];
    [_label_rengouPer setText:[NSString stringWithFormat:@"(%.2f%%)",roundf(offer*10000.0f/allMoney)/100]];
    [_label_FaTotalMoney setText:[NSString stringWithFormat:@"共%d彩豆",(offer+guarantee)]];
    [_label_buyMoneyDetail setText:[NSString stringWithFormat:@"认购%d彩豆+保底%d彩豆",offer,guarantee]];
    
}
-(void)setSecrecy{
    [_btnBmBaomi setSelected:false];
    [_btnBmFllowGongkai setSelected:false];
    [_btnBmGongkai setSelected:false];
    [_btnBmXiaoshouHgongkai setSelected:false];
    [_btnBmXSHFllowGongkai setSelected:false];
    switch (SecrecyLevel) {
        case 0:
            [_btnBmGongkai setSelected:true];
            break;
        case 1:
            [_btnBmXiaoshouHgongkai setSelected:true];
            break;
        case 2:
            [_btnBmFllowGongkai setSelected:true];
            break;
        case 3:
            [_btnBmXSHFllowGongkai setSelected:true];
            break;
        case 4:
            [_btnBmBaomi setSelected:true];
            break;
        default:
            break;
    }
}

-(void)setYongjin{
    [_btnBd0 setSelected:false];
    [_btnBd1 setSelected:false];
    [_btnBd2 setSelected:false];
    [_btnBd3 setSelected:false];
    [_btnBd4 setSelected:false];
    [_btnBd5 setSelected:false];
    [_btnBd6 setSelected:false];
    [_btnBd7 setSelected:false];
    [_btnBd8 setSelected:false];
    [_btnBd9 setSelected:false];
    [_btnBd10 setSelected:false];
    switch (BonusScale) {
        case 0:
            [_btnBd0 setSelected:true];
            break;
        case 1:
            [_btnBd1 setSelected:true];
            break;
        case 2:
            [_btnBd2 setSelected:true];
            break;
        case 3:
            [_btnBd3 setSelected:true];
            break;
        case 4:
            [_btnBd4 setSelected:true];
            break;
        case 5:
            [_btnBd5 setSelected:true];
            break;
        case 6:
            [_btnBd6 setSelected:true];
            break;
        case 7:
            [_btnBd7 setSelected:true];
            break;
        case 8:
            [_btnBd8 setSelected:true];
            break;
        case 9:
            [_btnBd9 setSelected:true];
            break;
        case 10:
            [_btnBd10 setSelected:true];
            break;
        default:
            break;
    }
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    return  YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return YES;
}

- (void)BDtextFieldDidChange:(id)sender{
    guarantee = [_tfBaoDi.text intValue];
    if ((guarantee + offer) >= allMoney) {
        [_tfBaoDi setText:[NSString stringWithFormat:@"%d",allMoney-offer]];
        [_btn_quanBao setSelected:true];
    }else{
        [_btn_quanBao setSelected:false];
    }
    [_label_baodiPer setText:[NSString stringWithFormat:@"(%.2f%%)",roundf(guarantee*10000.0f/allMoney)/100]];
    [_label_rengouPer setText:[NSString stringWithFormat:@"(%.2f%%)",roundf(offer*10000.0f/allMoney)/100]];
    [_label_FaTotalMoney setText:[NSString stringWithFormat:@"共%d彩豆",(offer+guarantee)]];
    [_label_buyMoneyDetail setText:[NSString stringWithFormat:@"认购%d彩豆+保底%d彩豆",offer,guarantee]];
}

- (void)RGtextFieldDidChange:(id)sender{
    offer = [_tfRenGou.text intValue];
    if(offer > allMoney){
        offer = allMoney;
        [_tfRenGou setText:[NSString stringWithFormat:@"%d",offer]];
    }
    
    if(_btn_quanBao.isSelected == true){
        guarantee = allMoney - offer;
        [_tfBaoDi setText:[NSString stringWithFormat:@"%d",guarantee]];
    }
    
    if ((offer+guarantee) > allMoney) {
        [_btn_quanBao setSelected:true];
        guarantee = allMoney - offer;
        [_tfBaoDi setText:[NSString stringWithFormat:@"%d",guarantee]];
    }
    [_label_baodiPer setText:[NSString stringWithFormat:@"(%.2f%%)",roundf(guarantee*10000.0f/allMoney)/100]];
    [_label_rengouPer setText:[NSString stringWithFormat:@"(%.2f%%)",roundf(offer*10000.0f/allMoney)/100]];
    [_label_FaTotalMoney setText:[NSString stringWithFormat:@"共%d彩豆",(offer+guarantee)]];
    [_label_buyMoneyDetail setText:[NSString stringWithFormat:@"认购%d彩豆+保底%d彩豆",offer,guarantee]];
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    NSLog(@"%@",@"textFieldShouldBeginEditing");
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
    tapGestureRecognizer.delegate = self;
    [self.bgScrollView addGestureRecognizer:tapGestureRecognizer];
//    [self.bgScrollView setContentOffset:CGPointMake(0, 213)animated:YES];
    return YES;
}
- (IBAction)handleSingleTapGesture:(UITapGestureRecognizer*)sender{
    NSLog(@"%@",@"handleSingleTapGesture");
    [_tfBaoDi resignFirstResponder];
    [_tfRenGou resignFirstResponder];
    [_TV_xuanyan resignFirstResponder];
    [self.bgScrollView removeGestureRecognizer:sender];
    [self.bgScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
    tapGestureRecognizer.delegate = self;
    [self.bgScrollView addGestureRecognizer:tapGestureRecognizer];
    [self.bgScrollView setContentOffset:CGPointMake(0, 213)animated:YES];
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:_btnFaqiHemai]) {
        return NO;
    }
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)tfEditingDone:(id)sender {
    [_tfBaoDi resignFirstResponder];
    [_tfRenGou resignFirstResponder];
}

- (IBAction)btnBDClick:(id)sender {
//    UIButton* btn = sender;
    BonusScale = ((UIButton*)sender).tag;
    [self setYongjin];
}

- (IBAction)btnBMClick:(id)sender {
    SecrecyLevel = ((UIButton*)sender).tag;
    [self setSecrecy];
}

- (IBAction)btn_QuanBaoClick:(id)sender {
    [_btn_quanBao setSelected:!_btn_quanBao.isSelected];
    if(_btn_quanBao.isSelected == true){
        guarantee = allMoney - offer;
    }else{
        guarantee = 0;
    }
    [self setvalue2Show];
}

- (IBAction)btnFaQiClick:(id)sender {
    
    offer = [_tfRenGou.text intValue];
    if(offer < leastMoney){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"                                                  message:[NSString stringWithFormat:@"认购金额需满总金额的5%%,至少%d彩豆",leastMoney]                                                           delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil,nil ];
        [alert show];
    }else{
        [JddCommonFunction setWaitingViewHidden:NO fromController:self];
        CoBuyLotteryOperation *operation2 = [[CoBuyLotteryOperation alloc] init];
        operation2.delegate = self;
        operation2.lotteryTypeId = [NSNumber numberWithInt:lotID];
        operation2.issueId = issueID;
        operation2.issueName = issueName;
        operation2.multiple = [NSNumber numberWithInt:bs];
        operation2.moneyAmount = [NSNumber numberWithFloat:allMoney];
        operation2.lotteryNumber = lotteryNumber;
        operation2.description = _TV_xuanyan.text;
        operation2.BonusScale = [NSNumber numberWithInt:BonusScale];
        operation2.guarantee = [NSNumber numberWithInt:guarantee];
        operation2.offer = [NSNumber numberWithInt:offer];
        operation2.SecrecyLevel = [NSNumber numberWithInt:SecrecyLevel];
        [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation2];
    }
}

-(void)CoBuyLotteryOperationResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        NSDictionary *ReDic = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:nil];
        if([[ReDic objectForKey:@"Code"] intValue] == 0)
        {
//            NSLog(@"%@",@"success");
//            // modify by rainy  修改投注使用接口不经过web
//            NSString * fanganhao = [ReDic objectForKey:@"Data"];
            alert1 = [JddAlertView getInstanceView];
//            alert1.lssueString = fanganhao;
//            alert1.moneylable = moneyAmountLabel.text;
//            [alert1 alertBuyMessage:[NSString stringWithFormat:@"双色球%@期",ls.name]];
            [alert1 alertFaQiCoBuyMessage:_issueNameTitle totalMoney:allMoney rengouMoeny:offer baodiMoney:guarantee];
//
//            //            NSString *checkUrlString = [NSString stringWithFormat:@"%@?ck=%@",BASE_SERVER_IOS_BUY_LOTTERY_URL,[ReDic objectForKey:@"Data"]];
//            //            NSURL *checkUrl = [NSURL URLWithString:checkUrlString];
//            //            [[UIApplication sharedApplication] openURL:checkUrl];
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"购彩失败"                                                  message:[ReDic objectForKey:@"Data"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil,nil ];
            [alert show];
        }
    }
}

@end
